package com.jingdianjichi.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 权限模块
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.add.dto: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()),"权限名称不能为空!");
            Preconditions.checkNotNull(authPermissionDTO.getType(),"权限类型不能为空!");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.instance.authPermissionDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error: {}",e.getMessage(),e);
            return Result.fail("新增权限失败");
        }

    }

    /**
     * 更新权限信息
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.update.dto: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空!");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.instance.authPermissionDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error: {}",e.getMessage(),e);
            return Result.fail("更新权限信息失败");
        }

    }

    /**
     * 删除权限信息
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.delete.authUserDto: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空!");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.instance.authPermissionDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.delete.error: {}",e.getMessage(),e);
            return Result.fail("删除权限失败");
        }

    }

    /**
     * 用户启用禁用
     */
    @RequestMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("PermissionController.changeStatus.dto: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空!");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.instance.authPermissionDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.changeStatus(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.changeStatus.error: {}",e.getMessage(),e);
            return Result.fail("用户启用禁用失败!");
        }

    }

    /**
     * 获取权限信息
     */
    @RequestMapping("/getPermission")
    public Result getPermission(@RequestParam("userName") String userName) {
        try {
            log.info("PermissionController.getPermission.username: {}", userName);
            Preconditions.checkArgument(StringUtils.isNotBlank(userName),"用户名不能为空");
            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error: {}",e.getMessage(),e);
            return Result.fail("用户启用禁用失败!");
        }

    }
    
}
